package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.annotation.NeedLogin;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @Operation(summary = "小程序授权登录")
    @GetMapping("/login/{code}")
    public Result<String> wxLogin(@PathVariable String code){
        return Result.ok(customerService.login(code));
    }

    @Operation(summary = "获取用户登录信息-已过时")
    @GetMapping("/getCustomerLoginInfo-hasDeprecated")
    @Deprecated  //这个接口是直接拿请求头的token来做处理的， 我们实现了验证登录的统一直接，可以用新的接口
    public Result<CustomerLoginVo> getCustomerLoginInfoHasDeprecated(@RequestHeader(value = "token")String token){
        //从请求头获取token   ,可以通过上面 @RequestHeader注解的方式从header里直接拿到token并且赋值给参数
        // String token = request.getHeader("token");
        return Result.ok(customerService.getCustomerLoginInfo(token));
    }

    @Operation(summary = "获取用户登录信息")
    @GetMapping("/getCustomerLoginInfo")
    @NeedLogin  //该接口做登录校验
    public Result<CustomerLoginVo> getCustomerLoginInfo(){
        log.info("从AuthContextHolder中获取用户id");
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(customerService.getCustomerLoginInfoNew(userId));
    }

    @Operation(summary = "更新用户微信手机号")
    @NeedLogin
    @PostMapping("/updateWxPhone")
    public Result<Boolean> updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm) {
        updateWxPhoneForm.setCustomerId(AuthContextHolder.getUserId());
        return Result.ok(customerService.updateWxPhoneNumber(updateWxPhoneForm));
    }
}

